
import { Tensor, TensorDataType, TensorShape } from "../Tensor";
import { AllocScope, ITensorAllocator } from "./ITensorAllocator";

export class TensorCachingAllocator implements ITensorAllocator {

    _allocatedTensors: Tensor[] = [];
    _name: string = "TensorCachingAllocator";

    allocTensorInternal(dataType: TensorDataType, shape: TensorShape, buffer: number[]): Tensor {

        let res: Tensor = null!;
        if (this._allocatedTensors.length > 0) {
            res = this._allocatedTensors.pop()!;
            res.dateType = dataType;
            res.shape = shape;
            res.cache = buffer;
            res.tensorAllocator = this;
        } else {
            res = new Tensor();
            res.dateType = dataType;
            res.shape = shape;
            res.cache = buffer;
            res.tensorAllocator = this;
        }
        return res;
    }

    alloc(shape: TensorShape, scope: AllocScope = AllocScope.LayerOutPut, dataType: TensorDataType = TensorDataType.Float): Tensor {
        const name = "untitled";
        const tensor = this.allocTensorInternal(dataType, shape, []);
        tensor.name = name;
        return tensor;
    }

    reset(arg0: boolean): unknown {
        throw new Error("Method not implemented.");
    }


}